using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using viBook.Models;

namespace viBook.Controllers
{
    public class PagesController : BaseController
    {
        #region Constructors and Repository vars

        private readonly IPageRepository pageRepository;

        // If you are using Dependency Injection, you can delete the following constructor
        public PagesController()
            : this(new PageRepository())
        {
        }

        public PagesController(IPageRepository pageRepository)
        {
            this.pageRepository = pageRepository;
        }

        #endregion


        public ActionResult Create()
        {
            return View();
        }

        public ActionResult Edit(int id)
        {
            var page = pageRepository.Find(id);
            return View(page);
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            int chapterId;
            pageRepository.Delete(id, out chapterId);
            pageRepository.Save();
            return RedirectToAction("Details", "Chapters", new { id = chapterId });
        }

        [HttpPost]
        public JsonResult SaveTags(List<int> deleted, List<Tag> added, int pageId)
        {
            pageRepository.SaveTags(deleted, added, pageId);
            return _successFeedback;
        }
    }
}

